Geomorph <- read.csv(here::here("/Geomorphology/Colmillo/2021-07-09 11_20_03 CANINE Stream Profile.csv"), skip = 42)
Geomorph <- Geomorph[,3:6]
#convert time in UTC to local time
Geomorph$time <- gsub("T"," ", Geomorph$time)
Geomorph$time <- gsub("Z","", Geomorph$time)
Geomorph$time <- as.POSIXct(Geomorph$time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC" )
Geomorph$time <-Geomorph$time - 5*60*60
#round to nearest half second
Geomorph$time <- round_date(Geomorph$time, unit = "30 second")
# 2021-07-16 START: 11:29:03 END: 15:24:37
Geomorph <- subset(Geomorph,
time > as.POSIXct('2021-07-09 11:29:03', tz="UTC") &
time < as.POSIXct('2021-07-09 15:24:37', tz="UTC"))
Geomorph_July9 <- Geomorph
Geomorph <- read.csv(here::here("/Geomorphology/Colmillo/COLMILLO 2021-07-16.csv"), skip = 42)
Geomorph <- Geomorph[,3:6]
#convert time in UTC to local time
Geomorph$time <- gsub("T"," ", Geomorph$time)
Geomorph$time <- gsub("Z","", Geomorph$time)
Geomorph$time <- as.POSIXct(Geomorph$time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC" )
Geomorph$time <-Geomorph$time - 5*60*60
#round to nearest half second
Geomorph$time <- round_date(Geomorph$time, unit = "30 second")
# 2021-07-16 START: 09:45:00 END: 13:49:17
Geomorph <- subset(Geomorph,
time < as.POSIXct('2021-07-16 13:49:17', tz="UTC") &
time > as.POSIXct('2021-07-16 09:44:00', tz="UTC"))
Geomorph <- rbind(Geomorph_July9,Geomorph)
##Plot the geomorphology data in 3-d
#are you seeing the problem? The synoptic data points seem to be fairly well geolocated for lat long, but not for elevation. Emphasise on fairly well, because these synoptics look a bit worse than ANTENAS. Unfortunatly.
So we need to build a smoothing function
#working with 3-D is hard. full stop. In this code chunk, we start by smooting a line to the lat lon data, which appears to be more accurate than the elevation
We will do this in parts though, be cause the relationship between lat lon can not be described as a function
Colmillo is not too far off from a funtion if lat is the “x” and lon is the ‘y’
Wow! that was so much easier than antenas!! Amazing.
now we have a smooth data frame of lat/lon data, we calculate the distance between each point
###create a loop and calculate distance along the river from the predict_df. THen we can bind in the GEOMORPH!
#df_prediction <- rbind(df1,df4,df2,df3) %>% drop_na(lat)
df_prediction <- df1
df_prediction$num <- NULL
#df_prediction <- df_prediction[,c("lon","lat")]
colnames(df_prediction) <- c("lat_fit","lon_fit")
df_prediction$lon_dist <- NA
df_prediction$lat_dist <- NA
i <- 2
for(i in 1:nrow(df_prediction)) {
if (i == 1) {
df_prediction$dist <- 0
# df_prediction[i,"lon_dist"] <- 0
# df_prediction[i,"lat_dist"] <- 0
} else {
df_prediction[i,"lon_dist"] <- df_prediction[i-1,"lon_fit"]
df_prediction[i,"lat_dist"] <- df_prediction[i-1,"lat_fit"]
df_prediction[i,"dist"] <- df_prediction[i-1,"dist"] + distHaversine(
c(df_prediction[i,"lon_fit"], df_prediction[i,"lat_fit"]),
c(df_prediction[i,"lon_dist"], df_prediction[i,"lat_dist"]))
}
}
snap the points in our origional dama frame to bring in the elevation data
#synop <- read.csv(here::here("Synoptic/COLMILLO_2021-08-30.csv"))
#synop <- read.csv(here::here("Synoptic/COLMILLO_2022-01-27.csv"))
#synop <- read.csv(here::here("Synoptic/COLMILLO_2022-02-14.csv"))
synop <- read.csv(here::here("Synoptic/COLMILLO_2022-03-24.csv"))
synop$X <- NULL
#df_prediction <- df_prediction[,c("lon_fit","lat_fit","ele")]
x <- SpatialPoints(Geomorph[,c(1:2)])
y <- SpatialPoints(df_prediction[,c(1:2)])
plot(y, col = "red")
points(y, col = "green")
snap = apply(spDists(x, y), 1, which.min)
points(y[snap,], pch = 3)
snap_df <- y[snap,]
coords_df <- snap_df@coords
pred <- as.data.frame(coords_df)
Geomorph$lon_fit <- pred$lon
Geomorph$lat_fit <- pred$lat
df_prediction <- full_join(df_prediction,Geomorph,by=c("lon_fit","lat_fit"))
## Warning: Ignoring 1 observations
plot in 3d
## No trace type specified:
## Based on info supplied, a 'scatter3d' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
ok sweet that looks really good
We also need to snap the synoptic df (for later)
#df_prediction <- rbind(df1,df2,df3,df4)
#df_prediction <- df_prediction[,c("lon","lat","num")]
x <- SpatialPoints(synop[,c(1:2)])
y <- SpatialPoints(df_prediction[,c(1:2)])
plot(x, col = "red")
points(y, col = "green")
snap = apply(spDists(x, y), 1, which.min)
points(y[snap,], pch = 3)
snap_df <- y[snap,]
coords_df <- snap_df@coords
pred <- as.data.frame(coords_df)
synop$lon_fit <- pred$lon
synop$lat_fit <- pred$lat
synop <- synop[,c("Date","EOS_no","Flux_ave","Point","CO2_ppm_ave","adjusted_ppm","lon_fit","lat_fit","AirTemp_c","VaisalaType","Total_hPa","COLM_waterTempAve")]
df_prediction <- full_join(df_prediction,synop,by=c("lon_fit","lat_fit"))
#Geomorph <- full_join(Geomorph,synop_2, by = c("lon_fit","lat_fit"))
##calculate distance between each coordinate. Here’s the deal. We need to calcualte the distance between each coordinate
First, lets find the most western lat point
now we have created a distance column that we can plot the elevation data against. lets see how that looks
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: Ignoring 407 observations
kriddie, you are doing so good! Okay, now lets fit another spline to this puppy
##loess spline i think loess might be a little better for this? IT IS BETTER!
df_prediction <- df_prediction %>% filter(dist > 0)
fit <- loess(ele ~ dist, degree=2, span = 0.20, data=df_prediction)
df_prediction %>%
drop_na(ele) %>%
mutate(smooth = fit$fitted) %>%
ggplot(aes(dist, ele)) +
geom_point(size = 3, alpha = .5, color = "grey") +
geom_line(aes(dist, smooth), color="red")
#fit <- loess(ele ~ dist, degree=2, span = 0.30, data=df_prediction)
distlims <- range(df_prediction$dist)
pred1 <- predict(fit, data.frame(dist = df_prediction$dist), se = TRUE)
df_prediction$ele_fit <- pred1$fit
#save <- df_prediction
#df_prediction <- save
#reverse order unfortunatly gavi is going from downstream to up, and we want upstream to down. so reverse this order you can’t just reverse order the distance column because they are not collected at even intervals.
#loops below switch order of distance
#calcualte slope here we calculate slope from the sample point and upstream 50m
take the loess spline and make a data frame that interpolates these data points
## Warning: 'scatter' objects don't have these attributes: 'z'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'